<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>表单元素</title>
</head>
<body>
    <div id="root"></div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        //onChange 不是 input 本身存在的事件, 是 React 定义的事件
        //当 input 元素的值发生改变的时候, 该事件就会触发
        class App extends React.Component{
            //1. 设置状态
            state = {
                username: '',
                password: '',
                gender: 'boy',
                hobby: [],
                city: '0',
                intro: ''
            }

            render(){
                //获取state中的两个属性
                let {username, password, gender, hobby, city, intro} = this.state;
                return <div className="container">
                        <h2>登录</h2>
                        <form action="">
                            {/*2. 添加value属性 以及 onChange 事件**/}
                            用户名: <input  type="text" value={username} onChange={this.saveData('username')} /> <br/>
                            密码: <input type="text" value={password} onChange={this.saveData('password')} /> <br/>
                            性别:  
                                男 <input type="radio" name="sex" value="boy"  onChange={this.genderChange}   checked={gender === 'boy'} />
                                女 <input type="radio" name="sex" value="girl" onChange={this.genderChange}   checked={gender === 'girl'} /><br />
                            爱好: 
                                抽烟 <input onChange={this.hobbyChange}  type="checkbox" name="hobby" value="抽烟" checked={hobby.includes('抽烟')}  />
                                喝酒 <input onChange={this.hobbyChange}  type="checkbox" name="hobby" value="喝酒" checked={hobby.includes('喝酒')}    />
                                烫头 <input onChange={this.hobbyChange}  type="checkbox" name="hobby" value="烫头" checked={hobby.includes('烫头')}     />
                                睡觉 <input onChange={this.hobbyChange}  type="checkbox" name="hobby" value="睡觉" checked={hobby.includes('睡觉')} /><br />
                                <br />
                            城市: 
                                <select name="" id="" value={city} onChange={this.cityChange}>
                                    <option value="0">不限</option>
                                    <option value="北京">北京</option>
                                    <option value="天津">天津</option>
                                    <option value="上海">上海</option>
                                    <option value="重庆">重庆</option>
                                    <option value="张家堡">张家堡</option>
                                </select><br />
                            个人介绍: <br />
                                <textarea name="" onChange={this.introChange} value={intro}  cols="30" rows="10"></textarea><br/>

                            <button onClick={this.login}>登录</button>
                        </form>
                    </div>
            }
            
            //文本域的处理
            introChange = (e) => {
                this.setState({
                    intro: e.target.value
                })
            }

            //下拉框的处理
            cityChange = (e) => {
                this.setState({
                    city: e.target.value
                })
            }

            //多选框的处理
            hobbyChange = (e) => {
                //解构赋值
                let {hobby} = this.state;
                // console.log(e.target.checked);
                //判断当前元素是否选中
                if(e.target.checked){
                    hobby.push(e.target.value);
                    //获取值
                    this.setState({
                        hobby: hobby
                    })
                }else{
                    // 获取当前元素值的下标
                    let v = e.target.value;
                    //获取元素在数组中的下标
                    let index = hobby.findIndex(item => item === v);
                    // let index = null;
                    // hobby.forEach((item, i) => {
                    //     if(item === v){
                    //         index = i;
                    //     }
                    // })
                    //删除
                    hobby.splice(index, 1);
                    //设置
                    this.setState({
                        hobby: hobby
                    })
                }
            }

            //input 文本框的处理
            saveData = (type) => {
                return (e) => {
                    this.setState({
                        [type] : e.target.value
                    })
                }
            }

            //单选框元素
            genderChange = (e) => {
                this.setState({
                    gender: e.target.value
                })
            }
            
            //登录
            login = (e) => {
                e.preventDefault()
                //获取用户名和密码的值
                let {username, password, gender, hobby, city, intro} = this.state;
                //输出
                console.log('用户名为: '+ username);
                console.log('密码为: '+ password);
                console.log('性别为: '+ gender);
                console.log('爱好: '+ hobby);
                console.log('城市: '+ city);
                console.log('个人介绍: '+ intro);
            }
        }

        ReactDOM.render(<App />, document.querySelector("#root"));
        
       
    </script>
</body>
</html>